package sortingHelper

import (
	"algorithms/insertionSort"
	"fmt"
	"golang.org/x/exp/constraints"
	"time"
)

func isSorted[E constraints.Ordered](arr []E) bool {
	for i := 1; i < len(arr); i++ {
		if arr[i-1] > arr[i] {
			return false
		}
	}
	return true
}

func SortTest[E constraints.Ordered](sortName string, n int, arr []E) error {

	startTime := time.Now().UnixNano()
	for k := 0; k < n; k++ {
		//if sortName == "SelectionSort" { //快速排序
		//	selectionSort.Sort0(arr)
		//} else
		if sortName == "InsertionSort0" { //插入排序
			insertionSort.Sort1(arr)
		} else if sortName == "InsertionSort2" {
			insertionSort.Sort2(arr)
		}
	}
	endTime := time.Now().UnixNano()
	time := float64(endTime-startTime) / 1000000000

	if !isSorted(arr) {
		return fmt.Errorf("%s failed", sortName)
	}
	fmt.Printf("%s , n = %d , %d runs : %f s \n", sortName, len(arr), n, time)
	return nil
}
